package com.dhcc.thymeleaf.utils;

import java.security.MessageDigest;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * <p>
 * 标题: UCloudUtil.java
 * </p>
 * <p>
 * 业务描述:健康管理平台
 * </p>
 * <p>
 * 公司:东华软件股份公司
 * </p>
 * <p>
 * 版权:dhcc2013
 * </p>
 * 
 * @author 胡凯
 * @date 2019年11月28日
 * @version V1.0
 */
@Component
public class UCloudUtil {
	/**
	 * sign签名
	 */
	public String createSign(SortedMap<Object, Object> packageParams, String privateKey) throws Exception {
		StringBuffer sb = new StringBuffer();
		Set es = packageParams.entrySet();
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			String v = (String) entry.getValue();
			sb.append(k + v);
		}
		sb.append(privateKey);
		String sign = shaEncode(sb.toString());
		return sign;
	}

	public String shaEncode(String inStr) throws Exception {
		MessageDigest sha = null;
		try {
			sha = MessageDigest.getInstance("SHA");
		} catch (Exception e) {
			System.out.println(e.toString());
			e.printStackTrace();
			return "";
		}

		byte[] byteArray = inStr.getBytes("UTF-8");
		byte[] md5Bytes = sha.digest(byteArray);
		StringBuffer hexValue = new StringBuffer();
		for (int i = 0; i < md5Bytes.length; i++) {
			int val = ((int) md5Bytes[i]) & 0xff;
			if (val < 16) {
				hexValue.append("0");
			}
			hexValue.append(Integer.toHexString(val));
		}
		return hexValue.toString();
	}

	@Value("${ucloud.publicKey}")
	private String publicKey;
	@Value("${ucloud.privateKey}")
	private String privateKey;
	@Value("${ucloud.resourceId}")
	private String resourceId;
	@Value("${ucloud.url}")
	private String ucloudUrl;
	@Value("${ucloud.yellow.flag}")
	private String yFlag;

	@Autowired
	RestTemplate restTemplate;
	/**
	 * ucloud 鉴黄
	 * @param imageUrl
	 * @return 返回值 RetCode 0 标识正常 其余一律异常 Suggestion 建议， pass-放行， forbid-封禁，
	 *         check-人工审核
	 */
	public String check(String imageUrl) {
		try {
			String toolsUrl = "https://bed.52itstyle.com/2019";
			// 图片绝对路径
			imageUrl = toolsUrl + imageUrl;
			HttpHeaders headers = new HttpHeaders();
			/**
			 * 生成signature，首字母排序
			 */
			String timestamp = System.currentTimeMillis() + "";
			SortedMap<Object, Object> packageParams = new TreeMap<>();
			packageParams.put("PublicKey", publicKey);
			packageParams.put("ResourceId", resourceId);
			packageParams.put("Timestamp", timestamp);
			packageParams.put("Url", imageUrl);
			String signature = createSign(packageParams, privateKey);
			/**
			 * 参数
			 */
			MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
			param.add("Scenes", "porn");
			param.add("Method", "url");//
			param.add("Url", imageUrl);
			/**
			 * headers 参数
			 */
			headers.setContentType(MediaType.parseMediaType("multipart/form-data; charset=UTF-8"));
			headers.set("PublicKey", publicKey);
			headers.set("Signature", signature);
			headers.set("ResourceId", resourceId);
			headers.set("Timestamp", timestamp);
			HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(param, headers);
			ResponseEntity<String> responseEntity = restTemplate.exchange(ucloudUrl, HttpMethod.POST, httpEntity, String.class);
			return responseEntity.getBody();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
}
